import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.Random;

public class Grid {
	public Card[][] cards = new Card[6][6];
	Image img = ImageLoader.images.get("grid");
	Image background;
	int x = 25;
	int y = 100;
	
	int dx = 47;
	int dy = 24;
	
	public Grid(int numberOfCards, Image bg) {
		background = bg;
		fillCards(numberOfCards);
	}

	public void fillCards(int numberOfCards) {
		ArrayList<Integer> pool = generatePool(numberOfCards);
		Random rng = new Random();

		for (int y = 0; y < cards.length; y++) {
			for (int x = 0; x < cards[0].length; x++) {
				int index = rng.nextInt(pool.size());
				int cardType = pool.get(index);
				pool.remove(index);
				
				switch (cardType) {
				case 1:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("redMushroomCard"));
					break;
				case 2:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("pigCard"));
					break;
				case 3:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("boneCard"));
					break;
				case 4:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("wheatCard"));
					break;
				case 5:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("lavaBucketCard"));
					break;
				case 6:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("cakeCard"));
					break;
				case 7:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("babyPigCard"));
					break;
				case 8:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("blazePowderCard"));
					break;
				case 9:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("spiderCard"));
					break;
				case 10:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("diamondSwordCard"));
					break;
				case 11:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("endermanCard"));
					break;
				case 12:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("goldIngotCard"));
					break;
				case 13:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("waterBucketCard"));
					break;
				case 14:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("swordCard"));
					break;
				case 15:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("tameWolfCard"));
					break;
				case 16:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("wolfCard"));
					break;
				case 17:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("torchCard"));
					break;
				case 18:
					cards[x][y] = new Card(this.x + dx + x * 69, this.y + dy + y * 69,
							ImageLoader.images.get("redAppleCard"));
					break;
				}
				
				
			}
		}
	}

	public ArrayList<Integer> generatePool(int numberOfCards) {
		ArrayList<Integer> pairs = new ArrayList<Integer>();
		int numberOfPairs = 18;
		int currentCard = 1;

		// Go through the entire array and fill it with cards
		for (int i = 0; i < numberOfPairs; i++) {
			pairs.add(currentCard++);

			if (currentCard > numberOfCards) {
				currentCard = 1;
			}
		}

		// Double pairs to convert from pairs to individual cards
		ArrayList<Integer> pool = new ArrayList<Integer>();

		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < numberOfPairs; j++) {
				pool.add(pairs.get(j));
			}
		}
		
		return pool;
	}

	public void draw(Graphics g) {
		g.drawImage(background, 0, 0, null);
		
		g.drawImage(img, x, y, null);
		
		for (int y = 0; y < cards.length; y++) {
			for (int x = 0; x < cards[0].length; x++) {
				Card temp = cards[x][y];
				g.drawImage(temp.currentImage, temp.x, temp.y, null);
			}
		}
	}
}
